package com.coral.login.module.quick;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.coral.login.common.result.IResult;
import com.coral.login.domain.entity.OrderInfo;
import com.coral.login.module.core.AbstractController;
import com.coral.login.module.core.annotation.ParameterMapping;
import com.coral.login.module.core.base.IExchargeParam;
import com.coral.login.module.core.support.AuthResult;
import com.coral.login.module.core.support.ErrorCodeEnum;
import com.coral.login.module.core.support.ExchargeResult;
import com.coral.login.module.core.support.ResultCode;
import com.coral.login.module.quick.param.QuickExchargeParam;
import com.coral.login.module.quick.param.QuickLoginParam;
import com.coral.login.util.HttpClientUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@RestController
@RequestMapping("/quick")
@ParameterMapping(loginParam = QuickLoginParam.class, 
exchargeParam = QuickExchargeParam.class)
public class QuickController extends AbstractController {

	@Autowired
	private QuickService service;

	@Override
	public IResult sdkLogin(Object object) {
		QuickLoginParam loginParam = (QuickLoginParam) object;
		
		final String userId = loginParam.getChannelUId();
		final String token = loginParam.getToken();
		IResult result = AuthResult.build(ErrorCodeEnum.ERROR_UNKNOWN);
		StringBuilder sb = new StringBuilder(666).append(QuickConfig.requestUrl);
		sb.append("token=").append(token).append("&uid=").append(userId).append("&product_code=").append(QuickConfig.productCode);
		try {
			String status = HttpClientUtil.doGet(sb.toString());
			// 验证失败,返回前端
			if (!StringUtils.equals(status, "1")) {
				return result;
			}
		} catch (Exception e) {
			error("Quick 验证出错, e:" + e);
		}
		return result;
	}
	
	@Override
	public IResult sdkExcharge(IExchargeParam object, OrderInfo orderInfo) {
		QuickExchargeParam param = (QuickExchargeParam) object;
		final String ntData = param.getNt_data();
		final String sign = param.getSign();
		final String md5Sign = param.getMd5Sign();
		if (!service.verifyOrder(ntData, sign, md5Sign)) {
			return ExchargeResult.build(ErrorCodeEnum.ERROR_UNKNOWN, ResultCode.CODE_SIGN_ERROR);
		}
		// 渠道参数转成对象便于使用
		QuickData data = service.strToQuickData(ntData);
		if (data == null) {
			return ExchargeResult.build(ErrorCodeEnum.ERROR_UNKNOWN, ResultCode.CODE_FAILD);
		}
		return ExchargeResult.build(ErrorCodeEnum.SUCCESS, ResultCode.CODE_SUCCESS);
	}


	public static void main(String[] args) {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><quicksdk_message><message><is_test>1</is_test><channel>0</channel><channel_uid>506067f729485b32d687eb3cd7d63fba@_()</channel_uid><channel_order></channel_order><game_order>9536F90D879041B3B45B9A5702D4ACB4</game_order><order_no>00020180904204119476215793</order_no><pay_time>2018-09-04 20:41:19</pay_time><amount>600.00</amount><status>0</status><extras_params>test</extras_params></message></quicksdk_message>";
		// String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"
		// standalone=\"no\"?><quicksdk_message><is_test>1</is_test><channel>0</channel><channel_uid>506067f729485b32d687eb3cd7d63fba@_()</channel_uid><channel_order></channel_order><game_order>9536F90D879041B3B45B9A5702D4ACB4</game_order><order_no>00020180904204119476215793</order_no><pay_time>2018-09-04
		// 20:41:19</pay_time><amount>600.00</amount><status>0</status><extras_params>test</extras_params></quicksdk_message>";
		xml = xml.replaceAll("\\<quicksdk_message>|\\</quicksdk_message>", "");
		System.out.println(xml);
		XStream xStream = new XStream(new DomDriver());
		// QuickStream xStream = new QuickStream();
		xStream.alias("message", QuickData.class);
		// xStream.omitField(QuickData.class, "quicksdk_message");
		// xStream.alias("message", QuickData.class);
		// System.out.println(xStream.fromXML(xml).toString());
		QuickData data = (QuickData) xStream.fromXML(xml);
		System.out.println("toBean:" + data);

	}

}
